﻿window.Order = Backbone.Model.extend({
    urlRoot: 'api/order/',
    url: function () {
        return this.urlRoot + localStorage.getItem('orderCode');
    },
    parse: function (model) {
        model.PaymentName = (model.Payment) && (model.Payment.NAME);
        model.DispatchModeName = (model.DispatchMode) && (model.DispatchMode.NAME);
        model.HasInvoice = model.InvoiceType != '';

        if (model.OderStatus == 5) {
            model.NeedPay = false;
            model.Status = "完成";
        } else {
            model.NeedPay = false;
            if (model.PaymentStatus == 2) {
                switch (model.ShipmentStatus) {
                    case 0:
                        model.Status = '未发货'; break;
                    case 1:
                        model.Status = '已发货'; break;
                    case 2:
                        model.Status = '已收货'; break;
                    case 3:
                        model.Status = '配货中'; break;
                    case 4:
                        model.Status = '已退货'; break;
                    default:
                        model.Status = '未发货'; break;
                }
            } else {
                switch (model.PaymentStatus) {
                    case 0:
                        model.Status = '未付款'; model.NeedPay = true; break;
                    case 1:
                        model.Status = '付款中'; break;
                    case 3:
                        model.Status = '退款'; break;
                    default:
                        model.Status = '未付款'; break;
                }
            }
        }
        return model;
    }
});

window.OrderList = Backbone.Collection.extend({
    model: Order,
    urlRoot: 'api/order/member/',
    url: function () {
        return this.urlRoot + pageView.getCookieValue('uid');
    }
});

window.MyOrderListView = Backbone.View.extend({
    el: '#jqt',
    initialize: function () {
        this.template = $('#MyOrderTemplate').html();
        this.collection = new OrderList();
    },
    events: {
        'click #MyOrder ul.orderItem': 'goToOrderDetail'
    },
    render: function () {
        var partial = { header: $('#HeaderTemplate').html(), footer: $('#FooterTemplate').html(),
            innerFooter: $('#InnerFooterTemplate').html()
        };
        var data = { hasBack: true, title: '返回', btnListR: [{ name: 'avatar' }, { name: 'home'}] };

        this.$el.append(Mustache.render(this.template, data, partial));

        this.fetchOrders();

        return this;
    },

    fetchOrders: function () {
        var that = this;
        this.collection.fetch({
            success: function () {
                var template;
                if (that.collection.length == 0) {
                    template = $('#EmptyOrderTemplate').html();
                    $('.myOrder').html(Mustache.render(template, [], []));
                } else {
                    template = $('#OrderListTemplate').html();
                    var data = { orders: that.collection.toJSON() };
                    $('.myOrder').html(Mustache.render(template, data, []));
                }
                pageView.resizeScroll();
            }
        });
    },

    goToOrderDetail: function (e) {
        var target = $(e.currentTarget);
        localStorage.setItem('orderCode', target.attr('orderCode'));
        pageView.goTo('OrderDetail');
    }
});